<template>
	<view>
		<nav-bars title="商品分销"></nav-bars>
		<view>
			<view class="searchBox">
				<u-search placeholder="请输入分销商品名称" placeholder-color="#939393" :show-action="false" v-model="keyword"
					@search="EnterClick" @clear="SearchClear" :animation="false">
				</u-search>
			</view>
			<view class="swiperBox">
				<swiper class="swiper" :indicator-dots="false" autoplay circular indicator-active-color="#C3AB7C"
					@change="SwiperChanege">
					<swiper-item v-for="(item,index) in Bannerlist" :key="index">
						<image :src="item.img"></image>
					</swiper-item>
				</swiper>
				<view class="SwiperDots" style="display: flex;align-items: center;">
					<view :class="[SwiperIndex == index?'SwiperActive':'SwiperItem']" v-for="(item,index) in Bannerlist"
						:key="index"></view>
				</view>
			</view>
			<!-- 筛选 -->
			<view :class="['selectBox',scrollTop >= 200?'selectBoxB':'']"
				:style="{top:scrollTop >= 200?navHeight+'px':0}">
				<u-dropdown menu-icon="arrow-down-fill" menu-icon-size="14" active-color="#A98F68"
					inactive-color="#333">
					<u-dropdown-item v-model="value1" :title="price" :options="options1" @change="ChangePrice">
					</u-dropdown-item>
					<u-dropdown-item v-model="value2" :title="profit" :options="options2" @change="ChangeProfit">
					</u-dropdown-item>
				</u-dropdown>
			</view>
			<view class="DomeList">
				<view style="display: flex;">
					<view>
						<view class="DomeItem" v-for="(dome,index) in leftList" :key="index"
							@click="GoDetaile(dome,index)" style="margin-right: 22rpx;">
							<view class="DomeImage">
								<image :src="dome.main_imgurl">
								</image>
							</view>
							<view class="DomeView">
								<view class="omit-2 DomeTitle">{{dome.product_title}}</view>
								<view style="font-size: 22rpx;font-weight: 500;color: #666666;margin-bottom: 10rpx;">
									{{dome.store_name}}
								</view>
								<view class="GoodsPirce" style="">
									<view class="GoodsPirceNum">
										<text class="GoodsPirceNumber" style="">¥ {{dome.price}}</text>
									</view>
									<view class="GoodsPirceNum">
										<text class="GoodsPirceNumber GoodsPirceVolume">销量: {{dome.volume}}</text>
									</view>
								</view>
							</view>
						</view>
					</view>
					<view>
						<view class="DomeItem" v-for="(goods,index) in rightList" :key="index"
							@click="GoDetaile(goods,index)">
							<view class="DomeImage">
								<image :src="goods.main_imgurl">
								</image>
							</view>
							<view class="DomeView">
								<view class="omit-2 DomeTitle">{{goods.product_title}}</view>
								<view style="font-size: 22rpx;font-weight: 500;color: #666666;margin-bottom: 10rpx;">
									{{goods.store_name}}
								</view>
								<view class="GoodsPirce">
									<view class="GoodsPirceNum">
										<text class="GoodsPirceNumber" style="">¥ {{goods.price}}</text>
									</view>
									<view class="GoodsPirceNum">
										<text class="GoodsPirceNumber GoodsPirceVolume">销量: {{goods.volume}}</text>
									</view>
								</view>
							</view>
						</view>
					</view>
				</view>
				<view v-if="flowList.length == 0" style="margin: 0 auto;">
					<u-empty text="暂无数据" mode="list" icon-size="550"
						src="https://cos.ryz1620.com/liankun/static/nullBg.png"></u-empty>
				</view>
				<view style="flex: 1;" v-if="flowList.length > 0 && isloadShow && total > 0">
					<u-loadmore :status="loadStatus"></u-loadmore>
				</view>
			</view>
		</view>
	</view>
</template>
<script>
	import service_distribution from '@/service/distribution.js'
	export default {
		name: 'list',
		components: {},
		data() {
			return {
				Bannerlist: [],
				SwiperIndex: 0,
				options1: [{
						label: '综合',
						value: null,
					},
					{
						label: '价格由高到低',
						value: 'desc',
					},
					{
						label: '价格由低到高',
						value: 'asc',
					},
				],
				options2: [{
						label: '综合',
						value: null,
					},
					{
						label: '销量由高到低',
						value: 'desc',
					},
					{
						label: '销量由低到高',
						value: 'asc',
					},
				],
				keyword: '',
				value1: '', //价格
				value2: '', //销量
				price: '价格',
				profit: '销量',
				scrollTop: 0,
				page: 1,
				flowList: [],
				leftList: [],
				rightList: [],
				isloadShow: false,
				loadStatus: 'loadmore',
				total:0,
			};
		},
		computed: {
			navHeight() {
				var navHeights = uni.getStorageSync('StatusBarHeight')
				return navHeights.height
			}, // Nav栏高度
		},
		onPageScroll(e) {
			this.scrollTop = e.scrollTop;
		},
		onLoad() {
			var that = this
			that.getBannerArr()
			that.getChangeBeforeList()
		},
		onShow() {
			var that = this

		},
		onReachBottom() {
			var that = this
			that.page++;
			that.isloadShow = true
			that.loadStatus = 'loading';
			uni.showLoading({
				title: '加载中...'
			})
			let params = {
				keyword: that.keyword,
				page: that.page,
				price: that.value1,
				volume: that.value2,
			}

			service_distribution.getGoodsList(params)
				.then(res => {
					if (res.code == 1) {
						that.total = res.data.total
						// that.flowList = res.data.products
						if (res.data.products.length > 0) {
							var list = res.data.products
							console.log(1);
							that.flowList = that.flowList.concat(res.data.products)
							
							if (list.length > 0) {
								console.log(2);
								list.forEach((item, index) => {
									console.log(3);
									if (index % 2 == 0) {
										// console.log(index);
										that.leftList.push(item)
										// that.rightList = [...that.rightList, item]
									} else {
										// console.log(index+'奇数');
										that.rightList.push(item)
										// that.leftList = [...that.leftList, item]
									}
								})
							}
						} else {
							that.loadStatus = 'nomore'
						}
						// console.log(res.data);
					} else {
						that.$utils.showErr({
							title: res.msg
						})
					}
				})
			setTimeout(function() {
				uni.hideLoading()
				// this.loadStatus = 'nomore';
			}, 500)
		},
		methods: {
			// 搜索
			EnterClick(e) {
				var that = this
				// console.log(e);
				if (e != '') {
					that.getChangeBeforeList()
				} else {
					that.$utils.showErr({
						title: '请输入分销商品名称'
					})
				}
			},
			// 清楚收缩内容
			SearchClear() {
				var that = this
				that.keyword = ''
				that.getChangeBeforeList()
			},
			// 轮播切换index
			SwiperChanege(e) {
				this.SwiperIndex = e.detail.current
			},
			// 价格筛选
			ChangePrice(e) {
				var that = this
				switch (e) {
					case null:
						that.price = '价格'
						break;
					case 'desc':
						that.price = '价格由高到低'
						break;
					case 'asc':
						that.price = '价格由低到高'
						break;
						// default:
				}
				that.getChangeBeforeList()
				// console.log(this.value1);
			},
			// 销量筛选
			ChangeProfit(e) {
				var that = this
				switch (e) {
					case null:
						that.profit = '销量'
						break;
					case 'desc':
						that.profit = '销量由高到低'
						break;
					case 'asc':
						that.profit = '销量由低到高'
						break;
						// default:
				}
				that.getChangeBeforeList()
			},
			// 获取轮播
			getBannerArr() {
				var that = this
				let params = {
					type: 33,
				}
				service_distribution.getBanner(params)
					.then(res => {
						if (res.code == 1) {
							that.Bannerlist = res.data
						} else {
							that.$utils.showErr({
								title: res.msg
							})
						}
					})
			},
			getChangeBeforeList() {
				var that = this
				that.page = 1
				// that.$refs.uWaterfall.clear();
				let params = {
					keyword: that.keyword,
					page: that.page,
					price: that.value1,
					volume: that.value2,
				}
				service_distribution.getGoodsList(params)
					.then(res => {
						if (res.code == 1) {
							that.flowList = res.data.products
							// console.log(res.data);
							var list = res.data.products
							that.total = res.data.total
							if (list.length > 0) {
								var left = []
								var right = []
								list.forEach((item, index) => {
									// console.log(index);
									if (index % 2 == 0) {
										left.push(item)
										// console.log(index);
										// that.rightList = [...that.rightList, item]
										// right.push(item)
									} else {
										right.push(item)
										// console.log(index+'奇数');
										// that.leftList = [...that.leftList, item]
										// left.push(item)
									}
								})
								that.leftList = left
								that.rightList = right
							} else {
								that.leftList = []
								that.rightList = []
							}
							// console.log(that.leftList);
							// console.log(that.rightList);
						} else {
							that.flowList = []
							that.leftList = []
							that.rightList = []
							that.$utils.showErr({
								title: res.msg
							});
						}
					})
			},
			GoDetaile(item, index) {
				uni.navigateTo({
					url: '/pages_makerMall/pages/detaile/detaile?product_id=' + item.product_id
				})
			},
		},
	};
</script>
<style lang="scss">
	page {
		background: #fff;
	}

	.searchBox {
		padding: 10rpx 32rpx 0rpx 32rpx;
	}

	.swiperBox {
		width: 690rpx;
		height: 300rpx;
		border-radius: 10rpx;
		overflow: hidden;
		margin: 30rpx auto 20rpx;
		box-shadow: 0px 4rpx 14rpx 0px #F1F1F1;
		position: relative;

		.swiper {
			image {
				width: 690rpx;
				height: 300rpx;
			}
		}

		.SwiperDots {
			position: absolute;
			bottom: 16rpx;
			left: 50%;
			transform: translate(-50%, 0%);
		}

		.SwiperItem {
			width: 12rpx;
			height: 12rpx;
			background: rgba(0, 0, 0, .3);
			border-radius: 100%;
			margin: 0 10rpx;
		}

		.SwiperActive {
			width: 24rpx;
			height: 12rpx;
			background: #C3AB7C;
			margin: 0 6rpx;
			border-radius: 14rpx;
		}
	}

	.selectBox {
		height: 80rpx;
		margin: 0rpx 0 0;
		background: #fff;
		border-bottom: 1px solid #fcfcfc;
		width: 100%;
		flex: 1;
		z-index: 1;
	}

	.selectBoxB {
		position: fixed;
		// top: 100rpx;
	}

	.DomeList {
		padding: 10rpx 32rpx 32rpx 32rpx;
		display: flex;
		align-items: flex-start;
		justify-content: space-between;
		flex-wrap: wrap;

		.DomeItem {
			box-shadow: 0px 2rpx 12rpx rgba(0, 0, 0, 0.07);
			width: 332rpx;
			margin-bottom: 20rpx;

			.DomeImage {
				width: 335rpx;
				height: 335rpx;
				overflow: hidden;
				border-radius: 8rpx 8rpx 0rpx 0rpx;
			}

			.DomeView {
				padding: 20rpx;

				.GoodsPirce {
					flex: 1;
					display: flex;
					align-items: center;
					justify-content: space-between;
				}

				.GoodsPirceNum {
					display: flex;
					align-items: flex-end;

					.GoodsPirceText {
						font-size: 20rpx;
						font-weight: 400;
						color: #FA4E37;
						margin-right: 10rpx;
					}

					.GoodsPirceNumber {
						font-size: 32rpx;
						font-weight: 400;
						color: #FA4E37;
						font-weight: 500;
					}

					.GoodsPirceVolume {
						color: #939393;
						font-size: 22rpx;
						display: flex;
					}
				}

				.DomeMain {
					margin-bottom: 20rpx;
					font-size: 20rpx;
					font-weight: 400;
					color: #939393;
				}

				.DomeTags {
					display: flex;
					align-items: center;
					margin-bottom: 20rpx;

					.DomeTagsItem {
						padding: 8rpx 16rpx;
						font-size: 20rpx;
						font-weight: 400;
						color: #A98F68;
						border-radius: 22rpx;
						background: #F9F6F3;
						width: max-content;
						margin-right: 16rpx;
					}
				}

				.DomeTitle {
					font-size: 30rpx;
					font-weight: bold;
					color: #333333;
					line-height: 36rpx;
					margin-bottom: 6rpx;
				}
			}
		}
	}
</style>
